<template>
  <div id="clock">
    <p class="date">{{ date }}</p>
    <p class="time">{{ time }}</p>
  </div>
</template>

<script setup>
import { ref ,onBeforeUnmount} from "vue";

const date = ref("");
const time = ref("");
const zeroPadding = (num, digit) => {
  var zero = "";
  for (var i = 0; i < digit; i++) {
    zero += "0";
  }
  return (zero + num).slice(-digit);
};
  let week = [
    "星期天",
    "星期一",
    "星期二",
    "星期三",
    "星期四",
    "星期五",
    "星期六",
  ];
const init = () => {
  let cd = new Date();
  time.value =
    zeroPadding(cd.getHours(), 2) +
    ":" +
    zeroPadding(cd.getMinutes(), 2) +
    ":" +
    zeroPadding(cd.getSeconds(), 2);
  date.value =
    zeroPadding(cd.getFullYear(), 4) +
    "-" +
    zeroPadding(cd.getMonth() + 1, 2) +
    "-" +
    zeroPadding(cd.getDate(), 2) +
    " " +
    week[cd.getDay()];
};
  let timerID = setInterval(init, 1000);
onBeforeUnmount(()=>{
  clearInterval(timerID);
  timerID=null
})
</script>

<style lang="less" scoped>
p {
  margin: 0;
  padding: 0;
}

#clock {
  display:flex;
  align-items: center;
  font-family: "Share Tech Mono", monospace;
  color: #ffffff;
  text-align: center;
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  color: #daf6ff;
  text-shadow: 0 0 20px #0aafe6, 0 0 20px rgba(10, 175, 230, 0);
}

#clock .date {
  letter-spacing: 0.1em;
  font-size: 24px;
}
#clock .time {
  margin-left:5px;
  letter-spacing: 0.05em;
  font-size: 24px;
  padding: 5px 0;
}
#clock .name {
  letter-spacing: 0.1em;
  font-size: 12px;
  padding: 20px 0 0;
}
</style>